package com.atguigu;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;



/*
 * 线程池
 * 
 * b
 */
public class ThreadDemon4 {

	public static void main(String[] args) {
		
		ScheduledExecutorService pool = Executors.newScheduledThreadPool(5);
		
		
//		try {
//			for (int i = 0; i < 20; i++) {
//				pool.schedule(new Runnable() {
//					
//					@Override
//					public void run() {
//						System.out.println(Thread.currentThread().getName());
//					}
//				}, 3, TimeUnit.SECONDS);
//			}
//			
//		} catch (Exception e) {
//			
//		}finally{
//			pool.shutdown();
//		}
//		
		
		try {
			for (int i = 0; i < 20; i++) {
				
				ScheduledFuture<Integer> future = pool.schedule(new Callable<Integer>() {

					@Override
					public Integer call() throws Exception {
						System.out.println(Thread.currentThread().getName());
						return new Random().nextInt(50);
					}
					
					
				}, 3, TimeUnit.SECONDS);
				System.out.println(future.get());
			}
		} catch (Exception e) {
			// TODO: handle exception
		}finally{
			
		}
		
		
		
	}
	
	
}

class Num3{
	
	private int i = 0;
	
	public void addNum(){
		System.out.println(Thread.currentThread().getName()+"---"+ ++i);
	}
	
	public void delNum(){
		System.out.println(Thread.currentThread().getName()+"---"+ ++i);
	}
	
}

